package com.njxzc.demo.service.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njxzc.demo.common.http.ResponseResult;
import com.njxzc.demo.entity.GoodsTypeEntity;
import com.njxzc.demo.mapper.admin.GoodsTypeMapper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class GoodsTypeServiceImpl extends ServiceImpl<GoodsTypeMapper, GoodsTypeEntity> implements GoodsTypeService{
    @Override
    public ResponseResult<Map<String, Object>> add(GoodsTypeEntity goodsTypeEntity) {
        boolean b = save(goodsTypeEntity);
        if (b)
            return ResponseResult.getMessageResult(null, "A001");
        return ResponseResult.getMessageResult(null, "A002");
    }

    @Override
    public ResponseResult<Map<String, Object>> update(GoodsTypeEntity goodsTypeEntity) {
        boolean b = updateById(goodsTypeEntity);
        if(b){
            return ResponseResult.getMessageResult(null, "A001");
        }
        return ResponseResult.getMessageResult(null, "A002");
    }

    @Override
    public ResponseResult<Map<String, Object>> delete(GoodsTypeEntity goodsTypeEntity) {
        boolean b = removeById(goodsTypeEntity);
        if(b){
            return ResponseResult.getMessageResult(null, "A001");
        }
        return ResponseResult.getMessageResult(null, "A002");
    }

    @Override
    public ResponseResult<Map<String, Object>> getGoodsType(GoodsTypeEntity goodsTypeEntity) {
        IPage<GoodsTypeEntity> iPage = new Page<>(goodsTypeEntity.getCurrentPage(), 2);
        //分页查询
        IPage<GoodsTypeEntity> page = page(iPage);
        Map<String, Object> myres = new HashMap<>();
        myres.put("allGoodsTypes", page.getRecords());
        myres.put("totalPage", page.getPages());
        return ResponseResult.getSuccessResult(myres);
    }

}
